package com.ht.controller;

import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.*;
import com.ht.service.ClinicalEvaluationService;
import com.ht.service.ArticleService;
import com.ht.service.PostsService;
import com.ht.service.ServiceEvaluationService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/evaluation")
@Api(value = "服务评价的功能")
public class EvaluationController {

    @Autowired
    private ServiceEvaluationService serviceEvaluationService;

    @Autowired
    private ClinicalEvaluationService clinicalEvaluationService;

    @Autowired
    private ArticleService favoriteService;

    @Autowired
    private PostsService postsService;

    @PostMapping("serviceEvaluation")
    //value为这个方法干什么用的
    @ApiOperation(value = "服务评价")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "serviceEvaluation",value = "用户在线服务评价",required = true,paramType = "body")
    })
    public Result createServiceEvaluation(@RequestHeader("AuthToken")String token, @RequestBody ServiceEvaluation serviceEvaluation){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        serviceEvaluationService.insert(user,serviceEvaluation);
        return Result.OK().setData("评价成功");
    }

    @PostMapping("clinicalEvaluation")
    //value为这个方法干什么用的
    @ApiOperation(value = "服务评价")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "clinicalEvaluation",value = "用户诊后评价的信息",required = true,paramType = "body")
    })
    public Result createClinicalEvaluation(@RequestHeader("AuthToken")String token, @RequestBody ClinicalEvaluation clinicalEvaluation){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        clinicalEvaluationService.insert(user,clinicalEvaluation);
        return Result.OK().setData("评价成功");
    }


    @GetMapping("selectArticle")
    //value为这个方法干什么用的
    @ApiOperation(value = "查询我收藏的文章")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header")
    public Result selectArticle(@RequestHeader("AuthToken")String token){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        List<Article> users = favoriteService.selectArticle(user);
        return Result.OK().setData(users);
    }

    @GetMapping("selectPosts")
    //value为这个方法干什么用的
    @ApiOperation(value = "查询用户收藏的帖子")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header")
    public Result selectPosts(@RequestHeader("AuthToken")String token){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        List<Posts> posts = postsService.selectPosts(user);
        return Result.OK().setData(posts);
    }
}
